package com.heima.big_event03.service.impl;

import com.heima.big_event03.entity.User;
import com.heima.big_event03.mapper.UserDao;
import com.heima.big_event03.service.UserService;
import com.heima.big_event03.utils.ThreadLocalUtil;
import org.springframework.stereotype.Service;
import org.springframework.util.DigestUtils;
//import org.springframework.data.domain.Page;
//import org.springframework.data.domain.PageImpl;
//import org.springframework.data.domain.PageRequest;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.Map;

/**
 * 用户表(User)表服务实现类
 *
 * @author makejava
 * @since 2025-03-04 20:44:40
 */
@Service("userService")
public class UserServiceImpl implements UserService {
    @Resource
    private UserDao userDao;

    /**
     * 通过ID查询单条数据
     *
     * @param id 主键
     * @return 实例对象
     */
    @Override
    public User queryById(String id) {
        return this.userDao.queryById(id);
    }


    /**
     * 修改数据
     *
     * @param user 实例对象
     * @return 实例对象
     */
    @Override
    public void update(User user) {
        user.setUpdateTime(LocalDateTime.now());
        this.userDao.update(user);
//        return this.queryById(user.getId());
    }


    /**
     * 根据username查询单条数据
     * @return
     */
    @Override
    public User queryByUserName(String username) {
        return this.userDao.queryByUserName(username);
    }

    /**
     * 注册
     * @param username
     * @param password
     */
    @Override
    public void register(String username, String password) {
        //密码加密
        password = DigestUtils.md5DigestAsHex(password.getBytes());
        this.userDao.register(username,password);
    }

    /**
     * 更新用户头像
     */
    @Override
    public void updateAvatar(String avatarUrl) {
        Map<String, Object> claims = ThreadLocalUtil.get();
        String id = (String) claims.get("id");
        User u = new User();
        u.setId(id);
        u.setUserPic(avatarUrl);
        u.setUpdateTime(LocalDateTime.now());
        this.userDao.update(u);
    }

    /**
     * 更新用户密码
     * @return
     */
    @Override
    public void updatePwd(User user) {
        this.userDao.update(user);
    }
}
